package com.capri.utils;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.capri.app.NurseHomeApp;

import java.util.UUID;

import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 设备信息工具类
 * Created by liukanshan on 2017/1/5.
 */

public class DeviceUtils {

    public static String GetDevcieId() {
        TelephonyManager TelephonyMgr = (TelephonyManager) NurseHomeApp.getInstance().getSystemService(TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String szImei = TelephonyMgr.getDeviceId();
        if (TextUtils.isEmpty(szImei)) {
            UUID uuid = UUID.randomUUID();
            szImei = uuid.toString();
        }
        return szImei;
    }

    public static boolean IsNetworkEnable() {
        ConnectivityManager connManager = (ConnectivityManager) NurseHomeApp.getInstance()
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return false;
        } else {
            return networkInfo.isAvailable();
        }
    }

    public static String getVersionName() {
        String pkName = NurseHomeApp.getInstance().getPackageName();
        String versionName = "";
        try {
            versionName = NurseHomeApp.getInstance().getPackageManager().getPackageInfo(
                    pkName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }


    public static String getVersionCode(){
        String pkName = NurseHomeApp.getInstance().getPackageName();
        int versionCode=0;
        try {
            versionCode = NurseHomeApp.getInstance().getPackageManager()
                    .getPackageInfo(pkName, 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return ""+versionCode;
    }

    public static String getDeviceInfo() {
        String phone_type = Build.MODEL;//手机型号
        int sdk = Build.VERSION.SDK_INT;
        String release = Build.VERSION.RELEASE;
        return phone_type + "#" + sdk + "#" + release;
    }
}
